package org.example.ch03.service;

import org.apache.ibatis.session.SqlSession;
import org.example.ch03.entity.User;
import org.example.ch03.mapper.UserMapper;
import org.example.ch03.util.MybatisUtils;

import java.util.List;
import java.util.Map;

/**
 * ClassName: UserService
 * Description:
 *
 * @Author zjl
 * @Create 2025/04/23 09:40
 * @Version 1.0
 */
public class UserService {
    public User getUserById(int id) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            return sqlSession.getMapper(UserMapper.class).getUserById(id);
        }
    }

    public List<User> listUsers() {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            return sqlSession.getMapper(UserMapper.class).listUsers();
        }
    }

    public Map<String, Object> getUserByName(String name) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            return sqlSession.getMapper(UserMapper.class).getUserByName(name);
        }
    }

    public List<Map<String, Object>> listUsersMap() {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            return sqlSession.getMapper(UserMapper.class).listUsersMap();
        }
    }
}
